#include <stdint.h>
#include "lgt8f690a.h"
#include "macros_auto.h"
#include "lgt8f690a_tc1.h"
#include "misc.h"
#include "function.h"
#include "event.h"

// default prescalar = MRC/4
// default system clock = 16MHz/4 = 4MHz
#define _XTAL_FREQ 4000000


void interrupt _L_HISR(void)
{
	if(TMR1IE & TMR1IF)
	{
		TMR1IF = 0;
		notifyper32ms();
	}
}

void setTc1(void)
{
	PR1L = TC1_PR1;
	PR1H = TC1_PR1 >> 8;
	T1CON = (TC1_T1GINV << 7) |
			(TC1_TGEN << 6) |
			(TC1_CKPS << 4) |
			(TC1_T1OSCEN << 3) |
			(TC1_T1SYNCB << 2) |
			(TC1_TMR1CS << 1);
	PIE1 |= TC1_T1IE;
	PEIE = 1;
}

int main()
{
#if 1	// in case of not trimmed
	if(cfw_valid())
		OSCTUNE = cfw_rctrim();
	else
		OSCTUNE = 0x92;
#endif
	//OSCCONbits.IRCF = 0x6;
	//MCUCR=0x55;
	//MCUCR=0xC6;
/*
	__delay_ms(1);
*/
	NOP();
	NOP();
	OPTION_REG &= 0x5f; //Global pp enable, Timer0 select iclk
	GIE = 0;

	initbankio();
	//do{
	//	LATB6 = 0; //set sytx low
		__delay_ms(60);
	//}while(LATB5 == 1);

	teston();
	__delay_ms(1000);

	testoff();
//TRISA6 = 0;

	setTc1();
	GIE = 1;
	TMR1ON = 1;
	__delay_ms(600);

	int8_t temp_top_base;
	int8_t temp_up_offset;

	int8_t temp_bot_base;
	int8_t temp_dn_offset;

	int8_t prsr_top_base;
	int8_t prsr_bot_base;
resetbank:
	temp_top_base = 0;
	temp_bot_base = 0;
	prsr_top_base = 0;
	prsr_bot_base = 0;
	showNumOnBank(TEMP_TOP, temp_top_base);
	showNumOnBank(PRSR_TOP, prsr_top_base);
	showNumOnBank(TEMP_BOT, temp_bot_base);
	showNumOnBank(PRSR_BOT, prsr_bot_base);
	while(1){
		if (0x8 & getEvent()){
			clrEvent(0x8);
			if (temp_top_base < 123){
				++temp_top_base;
				if (prsr_top_base < 16){
					prsr_top_base = temp_top_base >> 3;
				}
			}
			showNumOnBank(TEMP_TOP, temp_top_base);
			showNumOnBank(PRSR_TOP, prsr_top_base);

			if (temp_bot_base < 115){
				++temp_bot_base;
				if (prsr_bot_base < 10){
					prsr_bot_base = temp_bot_base >> 3;
				}
			}
			showNumOnBank(TEMP_BOT, temp_bot_base);
			showNumOnBank(PRSR_BOT, prsr_bot_base);
		}
		if ((temp_top_base >= 123) && (temp_bot_base >= 115))
		{
			break;
		}
	}
	while(1){

		if (0x20 & getEvent())
		{/* 1s */
			clrEvent(0x20);
			temp_up_offset = getRandom7();
			showNumOnBank(TEMP_TOP, 123 + temp_up_offset);
			if (temp_up_offset >= 1){
				showNumOnBank(PRSR_TOP, 16);
			}else if (temp_up_offset <= -2){
				showNumOnBank(PRSR_TOP, 14);
			}else{
				showNumOnBank(PRSR_TOP, 15);
			}
		}
		if (0x40 & getEvent())
		{/* 2s */
			clrEvent(0x40);
			temp_dn_offset = getRandom7();
			showNumOnBank(TEMP_BOT, 115 + temp_dn_offset);
			if (temp_dn_offset >= 1){
				showNumOnBank(PRSR_BOT, 10);
			}else if (temp_dn_offset <= -2){
				showNumOnBank(PRSR_BOT, 8);
			}else{
				showNumOnBank(PRSR_BOT, 9);
			}
		}

		if (0x100 & getEvent16())
		{
			clrEvent16(0x100);
			//LATA6 = ~LATA6;
			static uint8_t co=0;
			++co;
			if(3 == co){
				/* 24s */
				co = 0;
				testoff();
				__delay_ms(600);
				break;
			}
		}
	}

	goto resetbank;
	return 0;
}
